package com.hybao.pojo.dao;

import java.util.Map;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import com.hybao.pojo.ormaaping.UserInfo;

public class UserInfoDao {
	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public int getUserCountWithNameAndPwd(String name,String pwd){
		String sql = "select count(uid) from HYB_USER where USER_NICK = ? and USER_PASSWORD = ?";
		Object[] args = {name,pwd};
		return jdbcTemplate.queryForInt(sql, args);
	}
	
	public void createUserInfo(Map<String,String> userInfoMap){
		String sql = "insert into HYB_USER (GMT_CREATE,GMT_MODIFIED,USER_NICK,USER_PASSWORD,USER_ID,PHONE,BIRTHDATE,EMAIL) values (now(),now(),?,?,?,?,?,?)";
		Object[] args = {userInfoMap.get("userName"),userInfoMap.get("userPasswd"),userInfoMap.get("userId"),userInfoMap.get("phone"),userInfoMap.get("birth"),userInfoMap.get("userEmail")};
		jdbcTemplate.update(sql, args);
	}
	
	public UserInfo getUserInfoWithNick(String userNick){
		String sql = "select USER_NICK,USER_ID,PHONE,BIRTHDATE,EMAIL from HYB_USER where USER_NICK = ?";
		Object[] args = {userNick};
		try{
			return jdbcTemplate.queryForObject(sql, UserInfo.class, args);
		}
		catch(EmptyResultDataAccessException e){
			return null;
		}
	}
}
